<?

class Biz_Player{
    //获得全部Game_Player的数据
    function getAllPlayerKey(){
        for($i = ord("A") ; $i <= ord("Z") ; $i++ ){
            $list[] = chr($i);
        }
        return $list;
    }

    //获取某球员所有参加过的数据
    function getPlayerList($filter,$order,$page,$limit = 50,$team_add_url = false){
        $cache_key	= "AllPlayer_".md5(serialize($filter)."_".$order."_".$page."_".$limit."_".$all_attr);
        if($force === true) $data = Lib_Cache::get($cache_key);
        if(!$data){
            $player = new Data_Player();
            $num = $player->getPlayerListCount($filter);
            $num = intval($num['result'][0]['num']);
            if($num < 1) return array('num'=>0,'res'=>null);
            else $data['num'] = $num;
            $team_list = Biz_Team::getAllTeam();
            $res = $player->getPlayerList($filter,$order,$page,$limit);
            if(count($res['result']) >0){
                foreach ($res['result'] as $k=>$v){
                    if($v['teamid'] == 0){
                        $res['result'][$k]['team_name'] = '自由球员';
                    }else{
                        $res['result'][$k]['team_name'] = $team_list[$v['teamid']]['name'];
                    }
                }
                $data['res'] = $res['result'];
                Lib_Cache::set($cache_key,$data);
            }
        }
        return $data;
    }
    //获得全部Game_Player的数据
    function getAllPlayer($season,$force = false){
        $cache_key	= "AllPlayer_".$season;
        if($force === true) $data = Lib_Cache::get($cache_key);
        if(!$data){
            $player = new Data_Player();
            $res = $player->getAllPlayer($season);
            if(count($res['result']) >0){
                $data = $res['result'];
                $keys = array();
                foreach ($data as $per){
                    $per['player_url'] = Biz_Common::getURL('player',$per['reg_number'],$per['season']);
                    $rs[$per['name_first_letter']][] = $per;
                    if(!in_array($per['name_first_letter'],$keys)){
                        $keys[] = $per['name_first_letter'];
                    }
                }
                ksort($rs);
                sort($keys);
                $data = array('res'=>$rs,'key'=>$keys);
                Lib_Cache::set($cache_key,$data);
            }
        }
        return $data;
    }
    //获得所有球员的名称=>reg_number列表
    function getAllPlayerNameToReg($season){
        $cache_key	= "AllPlayer_name_reg_".$season;
        if($force === true) $data = Lib_Cache::get($cache_key);
        if(!$data){
            $player = self::getAllPlayer($season);
            foreach ($player['res'] as $value){
                foreach ($value as $per){
                    $per['name'] = trim(str_replace('被替换','',str_replace('(被替换','',str_replace('(被替换)','',trim($per['name'])))),'
                ');
                    $data[$per['name']] = $per['reg_number'];
                }
            }
        }
        return $data;
    }
    //获得所有球员的reg_number=>name列表
    function getAllPlayerRegToName($season){
        $cache_key	= "AllPlayer_reg_name_".$season;
        if($force === true) $data = Lib_Cache::get($cache_key);
        if(!$data){
            $player = self::getAllPlayer($season);
            foreach ($player['res'] as $value){
                foreach ($value as $per){
                    $per['name'] = trim(str_replace('被替换','',str_replace('(被替换','',str_replace('(被替换)','',trim($per['name'])))),'
                ');
                    $data[$per['reg_number']] = $per['name'];
                }
            }
        }
        return $data;
    }
    //根据首字母获取所有球员
    function getAllPlayerByFirstLetter($season,$first_letter,$force = false){
        $res = self::getAllPlayer($season);
        $data['res'][$first_letter] = $res['res'][$first_letter];
        $data['key'] = $res['key'];
        return $data;
    }
    //获取球员某赛季数据
    function getPlayerSchedule($reg_number,$season = null,$orderby = null,$limit = 20,$offset = 0){
        $cache_key	= "Player_schedule_".$reg_number."_".$season;
        if($force === true) $data = Lib_Cache::get($cache_key);
        if(!$data){
            $player = new Data_Player();
            $res = $player->getPlayerSchedule($reg_number,$season,$orderby,$limit,$offset);
            if(!$res['errorcode']){
                foreach ($res['result'] as $per){
                    $per['h_team_name'] = Biz_Team::name2short($per['h_team_name']);
                    $per['v_team_name'] = Biz_Team::name2short($per['v_team_name']);
                    $data[] = $per;
                }
                Lib_Cache::set($cache_key,$data);
            }
        }
        return $data;
    }
    //获取职业生涯
    function getPlayerDetail($reg_number,$season = null,$orderby = 'season asc'){
        $cache_key	= "PlayerDetail_".$reg_number."_".$season."_".$orderby;
        if($force === true) $data = Lib_Cache::get($cache_key);
        if(!$data){
            $player = new Data_Player();
            $res = $player->getPlayerDetail($reg_number,$season,$orderby);
            if(!$res['errorcode']){
                $data = $res['result'];
                //                print_z($data);
                Lib_Cache::set($cache_key,$data);
            }
        }
        return $data;
    }
    //根据球队获取球员
    function getPlayerByTeam($team_id,$season,$force = false){
        $cache_key	= "Player_TEAM_".$team_id."_".$season;
        if($force === true) $data = Lib_Cache::get($cache_key);
        if(!$data){
            $player = new Data_Player();
            $res = $player->getPlayerByTeam($team_id,$season);
            if(count($res['result']) >0){
                $data = $res['result'];
                foreach ($data as $per){
                    $reg_number_list[] = $per['reg_number'] = trim(addslashes($per['reg_number']));
                    $per['position'] = self::transPosition($per['position_id']);
                    $per['player_url'] = Biz_Common::getURL('player',$per['reg_number']);
                    $per['rank'] = 0;
                    $per['goal'] = 0;
                    $per['duration'] = 0;
                    $per['goal_efficiency'] = 0;
                    $per['assists'] = 0;
                    $per['assists_efficiency'] = 0;
                    $per['yellow'] = 0;
                    $per['red'] = 0;
                    $player_list[$per['reg_number']] = $per;
                    $order_list[] = $per['reg_number'];
                }
                $player_stats_list = $player->getPlayerStatById($reg_number_list,$season);
                if(count($player_stats_list['result']) >0)
                foreach ($player_stats_list['result'] as $per){
                    $player_list[$per['reg_number']]['rank'] = $per['rank'];
                    $player_list[$per['reg_number']]['goal'] = $per['goal'];
                    $player_list[$per['reg_number']]['duration'] = $per['duration'];
                    $player_list[$per['reg_number']]['goal_efficiency'] = $per['goal_efficiency'];
                    $player_list[$per['reg_number']]['assists'] = $per['assists'];
                    $player_list[$per['reg_number']]['assists_efficiency'] = $per['assists_efficiency'];
                    $player_list[$per['reg_number']]['yellow'] = $per['yellow'];
                    $player_list[$per['reg_number']]['red'] = $per['red'];
                }
                foreach ($order_list as $per){
                    $tmp[] = $player_list[$per];
                }
                $data = $tmp;
                Lib_Cache::set($cache_key,$data);
            }
        }
        return $data;
    }
    //获得球员信息
    function getPlayer($reg_number,$season,$force = false){
        $cache_key	= "Player_".$reg_number."_".$season;
        if($force === true) $data = Lib_Cache::get($cache_key);
        if(!$data){
            $player = new Data_Player();
            $res = $player->getPlayer($reg_number);
            //            print_z($res);
            if(count($res['result']) >0){
                $per = $res['result'][0];
                $per['reg_number'] = trim(addslashes($per['reg_number']));
                $per['position'] = self::transPosition($per['position_id']);
                $per['player_url'] = Biz_Common::getURL('player',$per['reg_number']);
                $per['team_info'] = Biz_Team::getTeam($per['team_id'],$season);
                $per['team_url'] = Biz_Common::getURL('team',$per['team_id'],$season);
                //                $per['nationality'] = strlen($per['nationality']<2)?'中国':$per['nationality'];
                $per['rank'] = 0;
                $per['goal'] = 0;
                $per['duration'] = 0;
                $per['goal_efficiency'] = 0;
                $per['assists'] = 0;
                $per['assists_efficiency'] = 0;
                $per['yellow'] = 0;
                $per['red'] = 0;
                $data = $per;
                $player_stats_list = $player->getPlayerStatById(array($per['reg_number']),$season);
                if(count($player_stats_list['result']) >0){
                    $per = $player_stats_list['result'][0];
                    $data['rank'] = $per['rank'];
                    $data['goal'] = $per['goal'];
                    $data['duration'] = $per['duration'];
                    $data['goal_efficiency'] = $per['goal_efficiency'];
                    $data['assists'] = $per['assists'];
                    $data['assists_efficiency'] = $per['assists_efficiency'];
                    $data['yellow'] = $per['yellow'];
                    $data['red'] = $per['red'];
                }
                Lib_Cache::set($cache_key,$data);
            }
        }
        return $data;
    }
    //获取排行
    function getRankBySeasonRound($season,$round,$type = 'goal',$limit=20,$offset=0){
        $cache_key	= "PlayerRank_".$round."_".$season."_".$type;
        $datas		= Lib_Cache::get($cache_key);
        if(!$datas){
            $player = new Data_Player();
            $rank = $player->getRank($season,$round,$type,$limit,$offset);
            if($rank['result']){
                $rank = $rank['result'];
                foreach ($rank as $key=>$per){
                    $rank[$key]['team_url'] = Biz_Common::getURL('team',$per['team_id'],$season);
                    $rank[$key]['player_url'] = Biz_Common::getURL('player',$per['reg_number'],$season);
                }
            }
            $datas = $rank;
            Lib_Cache::set($cache_key,$datas);
        }
        return $datas;
    }
    //获取排行
    function getRankBySeasonTeam($season,$team_id,$type = 'goal',$limit=10,$offset=0){
        $cache_key	= "PlayerRank_team_".$team_id."_".$season."_".$type;
        $datas		= Lib_Cache::get($cache_key);
        if(!$datas){
            $player = new Data_Player();
            $rank = $player->getRankByTeam($season,$team_id,$type,$limit,$offset);
            if($rank['result']){
                $rank = $rank['result'];
                foreach ($rank as $key=>$per){
                    $rank[$key]['team_url'] = Biz_Common::getURL('team',$per['team_id'],$season);
                    $rank[$key]['player_url'] = Biz_Common::getURL('player',$per['reg_number'],$season);
                }
            }
            $datas = $rank;
            Lib_Cache::set($cache_key,$datas);
        }
        return $datas;
    }
    //翻译位置
    function transPosition($position_id){
        $p_list = array('BD'=>'后卫','BW'=>'后卫','DF'=>'后卫','DM'=>'中场','DW'=>'后卫','FW'=>'前锋','GK'=>'守门员','GW'=>'前锋','MD'=>'中场','MF'=>'中场','MW'=>'中场');
        if(isset($p_list[$position_id])){
            return $p_list[$position_id];
        }else return '中场';
    }
}